//
//  CustomTabBarController.m
//  TabBarTest
//
//  Created by xc on 2016/10/27.
//  Copyright © 2016年 xc. All rights reserved.
//

// RGB颜色
#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

#import "CustomTabBarController.h"
#import "ViewController.h"
#import "CustomTabBar.h"
#import "UIView+Extension.h"

@interface CustomTabBarController ()<UITabBarDelegate>

{
    NSInteger tabbarIndex;
}
@end

@implementation CustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加子控制器
    [self addChildVc:[[SDClassViewController alloc] init] title:@"分类" image:@"classify_btn_def" selectedImage:@"classify_btn_sel"];
    [self addChildVc:[[SDShopingVC alloc] init] title:@"购物车" image:@"shop_btn_def" selectedImage:@"shop_btn_sel"];
    [self addChildVc:[[SDMainPageVC alloc] init] title:@"主页" image:@"home_btn_def" selectedImage:@"home_btn_sel"];
    [self addChildVc:[[SDPersonalCenterVC alloc] init] title:@"个人中心" image:@"personal_btn_def" selectedImage:@"personal_btn_sel"];
    [self addChildVc:[[SDHuiYuVC alloc] init] title:@"我的汇益" image:@"HY_btn_def" selectedImage:@"HY_btn_sel"];
    
    CustomTabBar *tabBar = [[CustomTabBar alloc] init];
    tabBar.delegate = self;
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    //设置默认加载items为2
    self.index = 2;
    [self setValue:tabBar forKey:@"tabBar"];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;
    
    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBColor(123, 123, 123)} forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBColor(220, 0, 0)} forState:UIControlStateSelected];
    //    childVc.view.backgroundColor = RandomColor; // 这句代码会自动加载主页，消息，发现，我 四个控制器的view，但是view要在我们用的时候去提前加载
    
    // 为子控制器包装导航控制器
    UINavigationController *navigationVc = [[UINavigationController alloc] initWithRootViewController:childVc];
    
    // 添加子控制器
    [self addChildViewController:navigationVc];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    CustomTabBar *cusTabBar = (CustomTabBar *)tabBar;
    NSInteger index = [cusTabBar.items indexOfObject:item];
    if (![FRUtils isLogin])
    {
        //未登录返回上一级tabbar
        if ((index == 0) || (index == 2))
        {
            self.index = index;
        }
    }
    if (index == 2) {
        cusTabBar.centerImage.highlighted = YES;
    }else{
        cusTabBar.centerImage.highlighted = NO;
    }
    if (index == 1)
    {
        if (![FRUtils isLogin])
        {
            [FRUtils presentToLoginViewControllerWithRootViewController:self];
        }

    }
    else if(index == 3)
    {
        if (![FRUtils isLogin])
        {
            [FRUtils presentToLoginViewControllerWithRootViewController:self];
        }
    }
    else if (index == 4)
    {
        if (![FRUtils isLogin])
        {
            [FRUtils presentToLoginViewControllerWithRootViewController:self];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
